package com.slwish.xty.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.slwish.common.base.PresenterFragment;
import com.slwish.common.config.Contants;
import com.slwish.common.config.PagerConfig;
import com.slwish.common.utils.IntentUtils;
import com.slwish.common.utils.LogUtils;
import com.slwish.xty.R;
import com.slwish.xty.adapter.CreditTourismListAdapter;
import com.slwish.xty.entity.CreditTourismInfoModel;
import com.slwish.xty.interf.OnFragmentInteractionListener;
import com.slwish.xty.mvp.contract.CreditTourismContract;
import com.slwish.xty.mvp.presenter.CreditTourismPresenter;
import com.slwish.xty.ui.activity.CreditTourismInfoActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 *
 */
public class SearchListFragment extends PresenterFragment<CreditTourismContract.Presenter>
        implements CreditTourismContract.View, XRecyclerView.LoadingListener {
    private PagerConfig mPagerCofig = new PagerConfig();
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    public static final String ARG_PARAM1 = "KEY";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.xrecyclerview)
    public XRecyclerView xrecyclerview;

    CreditTourismListAdapter mAdapter;
    List<CreditTourismInfoModel> mLists = new ArrayList<>();//列表数据集

    private String mParam1;
    private String mParam2;
    private OnFragmentInteractionListener mListener;

    public SearchListFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SearchListHistoryFragment.
     */
    public static SearchListFragment newInstance(String param1, String param2) {
        SearchListFragment fragment = new SearchListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            LogUtils.i("xx", "mParam1--->" + mParam1);

        }
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.frag_search_list;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        xrecyclerview.setLayoutManager();
        mAdapter = new CreditTourismListAdapter(mLists);
        xrecyclerview.setLoadingListener(this);
        xrecyclerview.setAdapter(mAdapter);

    }

    @Override
    public void onStart() {
        super.onStart();
        xrecyclerview.refresh();
    }

    @Override
    protected void initData() {
        super.initData();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt(Contants.ID, mAdapter.getItem(position - 1).getMerId());
                bundle.putSerializable(Contants.MODEL, mAdapter.getItem(position - 1));
                IntentUtils.startActivity(getContext(), CreditTourismInfoActivity.class, bundle);
            }
        });
    }

    @Override
    public void searchMerchantSuccess(List<CreditTourismInfoModel> model) {
//
        if (model != null && model.size() > 0) {
            if (mPagerCofig.getCurrentPage() == 1) {
                mLists.clear();
                xrecyclerview.refreshComplete();
            }
            mLists.addAll(model);
            xrecyclerview.setNoMore(false);
            if (mPagerCofig.getCurrentPage() != 1)
                mAdapter.notifyItemRangeChanged(mLists.size() - model.size(), mLists.size() - 1);

        } else {
            if (mPagerCofig.getCurrentPage() == 1) {
                mLists.clear();
                //显示无数据界面
                View view = View.inflate(getContext(), R.layout.layout_empty, null);
                mAdapter.setEmptyView(view);
                xrecyclerview.refreshComplete();
            }
            xrecyclerview.setNoMore(true);
        }
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 失败
     */
    @Override
    public void searchMerchantFailed() {
        if (mLists == null || mLists.size() < 1) {
            View view = View.inflate(getContext(), R.layout.layout_timeout, null);
            mAdapter.setEmptyView(view);
            xrecyclerview.refreshComplete();
            xrecyclerview.setNoMore(true);
            view.findViewById(R.id.tv_reset_loading).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    xrecyclerview.refresh();
                }
            });
        }
    }

    @Override
    protected CreditTourismContract.Presenter initPresenter() {
        return new CreditTourismPresenter(this);
    }

    @Override
    public void onRefresh() {
        mPagerCofig.setCurrentPage(1);
        mPresenter.searchMerchant(mParam1, mPagerCofig.getCurrentPage());
    }

    @Override
    public void onLoadMore() {
        //处理是否要加载分页数据
        if (mAdapter.getItemCount() / 10 == mPagerCofig.getCurrentPage()) {
            mPagerCofig.setCurrentPage(1 + mPagerCofig.getCurrentPage());
            mPresenter.searchMerchant(mParam1, mPagerCofig.getCurrentPage());//获取首页列表数据
        } else {
            xrecyclerview.setNoMore(true);
        }

    }

    public void onButtonPressed() {
        if (mListener != null) {
            mListener.onFragmentInteraction("");
        }
    }

    /**
     * 刷新
     * @param message
     */
    public void notifyList(String message) {
        mParam1 = message;
        xrecyclerview.refresh();

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void showError(String str) {

    }

}
